using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestApp
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void PopulateTree(TreeNode rootNode ,HtmlParser.NodeCollection nodes)
		{
			foreach(HtmlParser.Nodes.Node node in nodes)
			{
				if(node is HtmlParser.Nodes.Element)
				{
					HtmlParser.Nodes.Element element = node as HtmlParser.Nodes.Element;
					TreeNode treeNode = new TreeNode(element.TagName);
					treeNode.Tag = element;
					if (rootNode == null) treeView1.Nodes.Add(treeNode);
					else rootNode.Nodes.Add(treeNode);
					PopulateTree(treeNode, element.ChildNodes);
				}
			}			
		}

		private void textBox1_TextChanged(object sender, EventArgs e)
		{
			treeView1.Nodes.Clear();			
			HtmlParser.Document htmlDoc = new HtmlParser.Document(textBox1.Text);
			HtmlParser.ParserUtils.FixCommonUnfinishedTags(htmlDoc);
			HtmlParser.ParserUtils.MakeXmlValid(htmlDoc);
			PopulateTree(null,htmlDoc.RootNodes);
			treeView1.ExpandAll();			
		}

		private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			propertyGrid1.SelectedObject = e.Node.Tag;
		}

		private void treeView1_MouseClick(object sender, MouseEventArgs e)
		{
			
		}
	}
}